#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 定义收入和支出记录结构体
typedef struct {
    char date[11];       // 日期，格式：yyyy-mm-dd
    float amount;        // 金额大小
    char category[50];   // 类别（如工资、奖金等）
    char remark[100];    // 备注
} Bill;

// 全局变量
Bill income[100];       // 存储收入记录
Bill expense[100];      // 存储支出记录
int incomeCount = 0;    // 收入记录数量
int expenseCount = 0;   // 支出记录数量
float budget = 0.0;     // 月度预算
float totalIncome = 0.0, totalExpense = 0.0; // 总收入和总支出

// 函数声明
void showMenu();
void recordIncome();
void recordExpense();
void displayBills();
void queryBills();
void manageBudget();
void showStatistics();
int validateAmount(float amount);
void clearInputBuffer();

// 主函数
int main() {
    int choice;
    while (1) {
        showMenu();
        printf("请选择操作（输入数字）：");
        scanf("%d", &choice);
        clearInputBuffer();

        switch (choice) {
            case 1:
                recordIncome();
                break;
            case 2:
                recordExpense();
                break;
            case 3:
                displayBills();
                break;
            case 4:
                queryBills();
                break;
            case 5:
                manageBudget();
                break;
            case 6:
                showStatistics();
                break;
            case 7:
                printf("谢谢使用，退出系统！\n");
                exit(0);
            default:
                printf("无效的选项，请重新选择。\n");
        }
    }

    return 0;
}

// 显示主菜单
void showMenu() {
    printf("\n=== 个人账单管理系统 ===\n");
    printf("1. 记录收入\n");
    printf("2. 记录支出\n");
    printf("3. 显示所有账单\n");
    printf("4. 查询账单\n");
    printf("5. 设置预算\n");
    printf("6. 查看月度统计\n");
    printf("7. 退出\n");
    printf("========================\n");
}

// 记录收入
void recordIncome() {
    if (incomeCount >= 100) {
        printf("收入记录已满，无法添加更多记录。\n");
        return;
    }
    Bill newIncome;
    printf("请输入收入的日期 (格式: yyyy-mm-dd): ");
    fgets(newIncome.date, sizeof(newIncome.date), stdin);
    newIncome.date[strcspn(newIncome.date, "\n")] = 0;  // 去掉换行符

    while (1) {
        printf("请输入收入金额: ");
        scanf("%f", &newIncome.amount);
        clearInputBuffer();
        if (validateAmount(newIncome.amount)) {
            break;
        } else {
            printf("金额必须为正数，请重新输入。\n");
        }
    }

    printf("请输入收入类别（如工资、奖金等）: ");
    fgets(newIncome.category, sizeof(newIncome.category), stdin);
    newIncome.category[strcspn(newIncome.category, "\n")] = 0;

    printf("请输入备注: ");
    fgets(newIncome.remark, sizeof(newIncome.remark), stdin);
    newIncome.remark[strcspn(newIncome.remark, "\n")] = 0;

    income[incomeCount++] = newIncome;
    totalIncome += newIncome.amount;
    printf("收入记录已保存。\n");
}

// 记录支出
void recordExpense() {
    if (expenseCount >= 100) {
        printf("支出记录已满，无法添加更多记录。\n");
        return;
    }
    Bill newExpense;
    printf("请输入支出的日期 (格式: yyyy-mm-dd): ");
    fgets(newExpense.date, sizeof(newExpense.date), stdin);
    newExpense.date[strcspn(newExpense.date, "\n")] = 0;

    while (1) {
        printf("请输入支出金额: ");
        scanf("%f", &newExpense.amount);
        clearInputBuffer();
        if (validateAmount(newExpense.amount)) {
            break;
        } else {
            printf("金额必须为正数，请重新输入。\n");
        }
    }

    printf("请输入支出类别（如餐饮、交通等）: ");
    fgets(newExpense.category, sizeof(newExpense.category), stdin);
    newExpense.category[strcspn(newExpense.category, "\n")] = 0;

    printf("请输入备注: ");
    fgets(newExpense.remark, sizeof(newExpense.remark), stdin);
    newExpense.remark[strcspn(newExpense.remark, "\n")] = 0;

    expense[expenseCount++] = newExpense;
    totalExpense += newExpense.amount;
    printf("支出记录已保存。\n");
}

// 显示所有账单
void displayBills() {
    printf("\n--- 收入记录 ---\n");
    for (int i = 0; i < incomeCount; i++) {
        printf("%s | %.2f | %s | %s\n", income[i].date, income[i].amount, income[i].category, income[i].remark);
    }

    printf("\n--- 支出记录 ---\n");
    for (int i = 0; i < expenseCount; i++) {
        printf("%s | %.2f | %s | %s\n", expense[i].date, expense[i].amount, expense[i].category, expense[i].remark);
    }
}

// 查询账单
void queryBills() {
    char queryDate[11];
    printf("请输入查询的日期 (格式: yyyy-mm-dd), 留空查询所有: ");
    fgets(queryDate, sizeof(queryDate), stdin);
    queryDate[strcspn(queryDate, "\n")] = 0;

    printf("\n--- 收入记录 ---\n");
    for (int i = 0; i < incomeCount; i++) {
        if (strlen(queryDate) == 0 || strcmp(income[i].date, queryDate) == 0) {
            printf("%s | %.2f | %s | %s\n", income[i].date, income[i].amount, income[i].category, income[i].remark);
        }
    }

    printf("\n--- 支出记录 ---\n");
    for (int i = 0; i < expenseCount; i++) {
        if (strlen(queryDate) == 0 || strcmp(expense[i].date, queryDate) == 0) {
            printf("%s | %.2f | %s | %s\n", expense[i].date, expense[i].amount, expense[i].category, expense[i].remark);
        }
    }
}

// 设置预算
void manageBudget() {
    printf("请输入本月的支出预算: ");
    scanf("%f", &budget);
    clearInputBuffer();
    printf("预算已设置为: %.2f\n", budget);
}

// 查看月度统计
void showStatistics() {
    printf("\n--- 月度统计 ---\n");
    printf("总收入: %.2f\n", totalIncome);
    printf("总支出: %.2f\n", totalExpense);
    printf("剩余预算: %.2f\n", budget - totalExpense);
}


// 检查金额是否合法
int validateAmount(float amount) {
    return amount > 0;
}

// 清空输入缓冲区
void clearInputBuffer() {
    while (getchar() != '\n');
}

